//
//  UILayer+Extesion.swift
//  Teamo
//
//  Created by mdzz on 2021/12/10.
//

import Foundation
import UIKit

public enum GradientDirection {
    case leftToRight
    case topToBottom
}

extension CALayer {
    
    
    /// 添加渐变色layer
    /// - Parameters:
    ///   - colors: 颜色数组
    ///   - direction: 渐变方向 （e.g. 左-右）
    public func addGradient(colors: [CGColor]? = [UIColor.clear.cgColor],direction: GradientDirection? = .leftToRight) {
        let gradient = CAGradientLayer()
        gradient.frame = self.bounds
        gradient.colors = colors
        
        switch direction {
        case .leftToRight:
            gradient.startPoint = CGPoint(x: 0, y: 0)
            gradient.endPoint = CGPoint(x: 1, y: 0)
        case .topToBottom:
            gradient.startPoint = CGPoint(x: 0, y: 0)
            gradient.endPoint = CGPoint(x: 0, y: 1)
        case .none:
            gradient.startPoint = CGPoint(x: 0, y: 0)
            gradient.endPoint = CGPoint(x: 1, y: 0)
        }
        self.addSublayer(gradient)
        
    }
}
